package org.sanguine.study.hometasks.second;

import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class Task8 {
	
	private static Map<Character, Character> brackets;
	
	static {
		brackets = new HashMap<>(6);
		
		brackets.put('(', null);
		brackets.put('[', null);
		brackets.put('{', null);
		brackets.put(')', '(');
		brackets.put(']', '[');
		brackets.put('}', '{');
		brackets = Collections.unmodifiableMap(brackets);
	}
	
	public static boolean checkBracketPairs(String expressionText) {
		
		Deque<Character> deque = new LinkedList<>();
		
		if (expressionText == null || expressionText.isEmpty()) return true;

		for (int i = 0; i < expressionText.length(); i++) {
			Character ch = expressionText.charAt(i);
			if (!brackets.containsKey(ch)) continue;
			Character pairBracket = brackets.get(ch); 
			if (pairBracket == null) {
				deque.addFirst(ch);
			} else {
				Character beginingBracket = deque.pollFirst();
				if (beginingBracket == null || !pairBracket.equals(beginingBracket)) return false;
			}
		}
		return deque.isEmpty();
	}

}
